<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 根节点 -->
        <tree-tools :is-root="true" :node-data="company" @add-depts="handleAddDepts" />
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <template #default="{data}">
            <!-- 子节点 -->
            <tree-tools :node-data="data" @edit-depts="handleEditDepts" @delfu="getDepartments" @add-depts="handleAddDepts" />
          </template>
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="addDeptDialog" :node-data="nodeData" :depts="depts" :dialog-visible="dialogVisible" @tijiao="getDepartments" @close-dialog="closeDialogFn" />
  </div>
</template>

<script>
import { reqDepartments } from '@/api/departments'
import treeTools from './components/tree-tools.vue'
import { listToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: { treeTools, AddDept },
  data() {
    return {
      loading: false,
      dialogVisible: false, // 是否显示模态框
      company: {}, // 卡片的头部内容
      departs: [], // 转换好的树状数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      nodeData: {}, // 点击后获取当前点击的那一项的数据，每一行都是nodeData
      depts: []// 列表式数据
    }
  },
  created() {
    // 一进入页面就调用获取组织架构数据的方法
    this.getDepartments()
  },
  methods: {
    // 点击弹出新增部门的模态框
    handleAddDepts(nodeData) {
      this.dialogVisible = true
      // console.log(nodeData)
      this.nodeData = nodeData
    },
    // 关闭新增部门的模态框
    closeDialogFn(flag) {
      this.dialogVisible = flag
    },
    // 点击弹出编辑部门的模态框
    handleEditDepts(nodeData) {
      // 弹出模态框
      this.dialogVisible = true
      // 让在data函数中定义的nodeData赋值接受到的nodeData
      this.nodeData = nodeData
      // 通过在组件上设置ref值可以调用组件内部的方法
      this.$refs.addDeptDialog.getgetEmployeeSimple()
      // 在这里调用方法, 让子组件, 获取部门详情的数据
      this.$refs.addDeptDialog.getDepartDetail(nodeData.id)
    },
    // 获取组织架构的数据
    async getDepartments() {
      this.loading = true
      // 发送请求，得到res里的数据，结构res
      const { data } = await reqDepartments()
      // console.log(data)
      // 将响应回来的数据赋值给data定义的空数组company
      this.company = {
        name: data.companyName,
        manager: '负责人',
        id: ''
      }
      // 调用上面的函数传入两个实参
      // list就是data.depts 获取的列表数据
      // flag是''
      // 需要将其转换成树形结构
      this.departs = listToTreeData(data.depts, '')
      // 列表数据
      this.depts = data.depts
      // console.log(this.departs)
      this.loading = false
    }
  }
}
</script>

  <style scoped lang="scss">
  .tree-card {
  padding: 30px 30px;
  font-size:14px;
}
    .el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
  </style>
